<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>BTree.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="BTree code in Java">
<meta NAME="TITLE" CONTENT="BTree code in Java">
<meta NAME="KEYWORDS" CONTENT="BTree,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>BTree.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "BTree.java">BTree.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac BTree.java</span>
<span class="comment"> *  Execution:    java BTree</span>
<span class="comment"> *  Dependencies: StdOut.java</span>
<span class="comment"> *</span>
<span class="comment"> *  B-tree.</span>
<span class="comment"> *</span>
<span class="comment"> *  Limitations</span>
<span class="comment"> *  -----------</span>
<span class="comment"> *   -  Assumes M is even and M &gt;= 4</span>
<span class="comment"> *   -  should b be an array of children or list (it would help with</span>
<span class="comment"> *      casting to make it a list)</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">BTree</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents an ordered symbol table of generic</span>
<span class="comment"> *  key-value pairs.</span>
<span class="comment"> *  It supports the </span><span class="keyword">&lt;em&gt;</span><span class="comment">put</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">get</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">contains</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">size</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">is-empty</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> methods.</span>
<span class="comment"> *  A symbol table implements the </span><span class="keyword">&lt;em&gt;</span><span class="comment">associative array</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> abstraction:</span>
<span class="comment"> *  when associating a value with a key that is already in the symbol table,</span>
<span class="comment"> *  the convention is to replace the old value with the new value.</span>
<span class="comment"> *  Unlike {</span><span class="type">@link</span><span class="comment"> java.util.Map}, this class uses the convention that</span>
<span class="comment"> *  values cannot be </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="preproc">&amp;mdash;</span><span class="comment">setting the</span>
<span class="comment"> *  value associated with a key to </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is equivalent to deleting the key</span>
<span class="comment"> *  from the symbol table.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses a B-tree. It requires that</span>
<span class="comment"> *  the key type implements the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Comparable</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> interface and calls the</span>
<span class="comment"> *  </span><span class="keyword">&lt;tt&gt;</span><span class="comment">compareTo()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and method to compare two keys. It does not call either</span>
<span class="comment"> *  </span><span class="keyword">&lt;tt&gt;</span><span class="comment">equals()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">hashCode()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">get</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">put</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">contains</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operations</span>
<span class="comment"> *  each make log</span><span class="keyword">&lt;sub&gt;&lt;em&gt;</span><span class="comment">M</span><span class="keyword">&lt;/em&gt;&lt;/sub&gt;</span><span class="comment">(</span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) probes in the worst case,</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of key-value pairs</span>
<span class="comment"> *  and </span><span class="keyword">&lt;em&gt;</span><span class="comment">M</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the branching factor.</span>
<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">size</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">is-empty</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operations take constant time.</span>
<span class="comment"> *  Construction takes constant time.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see</span>
<span class="comment"> *  </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/62btree"</span><span class="keyword">&gt;</span><span class="comment">Section 6.2</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">BTree</span><span class="symbol">&lt;</span><span class="normal">Key </span><span class="usertype">extends</span><span class="normal"> Comparable</span><span class="symbol">&lt;</span><span class="normal">Key</span><span class="symbol">&gt;,</span><span class="normal"> Value</span><span class="symbol">&gt;</span><span class="normal">  </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="comment">// max children per B-tree node = M-1</span>
<span class="normal">    </span><span class="comment">// (must be even and greater than 2)</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> M </span><span class="symbol">=</span><span class="normal"> </span><span class="number">4</span><span class="symbol">;</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> root</span><span class="symbol">;</span><span class="normal">       </span><span class="comment">// root of the B-tree</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> height</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// height of the B-tree</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal">           </span><span class="comment">// number of key-value pairs in the B-tree</span>

<span class="normal">    </span><span class="comment">// helper B-tree node data type</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Node</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> m</span><span class="symbol">;</span><span class="normal">                             </span><span class="comment">// number of children</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> Entry</span><span class="symbol">[]</span><span class="normal"> children </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Entry</span><span class="symbol">[</span><span class="normal">M</span><span class="symbol">];</span><span class="normal">   </span><span class="comment">// the array of children</span>

<span class="normal">        </span><span class="comment">// create a node with k children</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> k</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            m </span><span class="symbol">=</span><span class="normal"> k</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// internal nodes: only use key and next</span>
<span class="normal">    </span><span class="comment">// external nodes: only use key and value</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Entry</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Comparable</span><span class="normal"> key</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Object</span><span class="normal"> val</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> next</span><span class="symbol">;</span><span class="normal">     </span><span class="comment">// helper field to iterate over array entries</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Entry</span><span class="symbol">(</span><span class="usertype">Comparable</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Object</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> next</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">key  </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">val  </span><span class="symbol">=</span><span class="normal"> val</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">next </span><span class="symbol">=</span><span class="normal"> next</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an empty B-tree.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">BTree</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        root </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="normal"> </span>
<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if this symbol table is empty.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if this symbol table is empty; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isEmpty</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of key-value pairs in this symbol table.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of key-value pairs in this symbol table</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> N</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the height of this B-tree (for debugging).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the height of this B-tree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">height</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> height</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the value associated with the given key.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the value associated with the given key if the key is in the symbol table</span>
<span class="comment">     *         and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the key is not in the symbol table</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">key</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"key must not be null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">search</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> </span><span class="function">search</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> ht</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        Entry</span><span class="symbol">[]</span><span class="normal"> children </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// external node</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ht </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">eq</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">].</span><span class="normal">key</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Value</span><span class="symbol">)</span><span class="normal"> children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">].</span><span class="normal">val</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// internal node</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">j</span><span class="symbol">+</span><span class="number">1</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">m </span><span class="symbol">||</span><span class="normal"> </span><span class="function">less</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">].</span><span class="normal">key</span><span class="symbol">))</span>
<span class="normal">                    </span><span class="keyword">return</span><span class="normal"> </span><span class="function">search</span><span class="symbol">(</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">].</span><span class="normal">next</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> ht</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Inserts the key-value pair into the symbol table, overwriting the old value</span>
<span class="comment">     * with the new value if the key is already in the symbol table.</span>
<span class="comment">     * If the value is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, this effectively deletes the key from the symbol table.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  key the key</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  val the value</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">key</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">put</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"key must not be null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> u </span><span class="symbol">=</span><span class="normal"> </span><span class="function">insert</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">);</span><span class="normal"> </span>
<span class="normal">        N</span><span class="symbol">++;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">u </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// need to split root</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> t </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">);</span>
<span class="normal">        t</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Entry</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">,</span><span class="normal"> root</span><span class="symbol">);</span>
<span class="normal">        t</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Entry</span><span class="symbol">(</span><span class="normal">u</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">,</span><span class="normal"> u</span><span class="symbol">);</span>
<span class="normal">        root </span><span class="symbol">=</span><span class="normal"> t</span><span class="symbol">;</span>
<span class="normal">        height</span><span class="symbol">++;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">insert</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> h</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> ht</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> j</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">Entry</span><span class="normal"> t </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Entry</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// external node</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ht </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">less</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">].</span><span class="normal">key</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">break</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// internal node</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">j</span><span class="symbol">+</span><span class="number">1</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> </span><span class="function">less</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">].</span><span class="normal">key</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="usertype">Node</span><span class="normal"> u </span><span class="symbol">=</span><span class="normal"> </span><span class="function">insert</span><span class="symbol">(</span><span class="normal">h</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">++].</span><span class="normal">next</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> ht</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">                    </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">u </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">                    t</span><span class="symbol">.</span><span class="normal">key </span><span class="symbol">=</span><span class="normal"> u</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="normal">key</span><span class="symbol">;</span>
<span class="normal">                    t</span><span class="symbol">.</span><span class="normal">next </span><span class="symbol">=</span><span class="normal"> u</span><span class="symbol">;</span>
<span class="normal">                    </span><span class="keyword">break</span><span class="symbol">;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&gt;</span><span class="normal"> j</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">--)</span>
<span class="normal">            h</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        h</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> t</span><span class="symbol">;</span>
<span class="normal">        h</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">++;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">h</span><span class="symbol">.</span><span class="normal">m </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">         </span><span class="keyword">return</span><span class="normal"> </span><span class="function">split</span><span class="symbol">(</span><span class="normal">h</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// split node in half</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">split</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> h</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> t </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="normal">M</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">);</span>
<span class="normal">        h</span><span class="symbol">.</span><span class="normal">m </span><span class="symbol">=</span><span class="normal"> M</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span>
<span class="normal">            t</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">M</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">+</span><span class="normal">j</span><span class="symbol">];</span><span class="normal"> </span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> t</span><span class="symbol">;</span><span class="normal">    </span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a string representation of this B-tree (for debugging).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a string representation of this B-tree.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">toString</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">toString</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> </span><span class="string">""</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">toString</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> h</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> ht</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> indent</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">StringBuilder</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">StringBuilder</span><span class="symbol">();</span>
<span class="normal">        Entry</span><span class="symbol">[]</span><span class="normal"> children </span><span class="symbol">=</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">children</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ht </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                s</span><span class="symbol">.</span><span class="function">append</span><span class="symbol">(</span><span class="normal">indent </span><span class="symbol">+</span><span class="normal"> children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">].</span><span class="normal">key </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">].</span><span class="normal">val </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> h</span><span class="symbol">.</span><span class="normal">m</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">j </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> s</span><span class="symbol">.</span><span class="function">append</span><span class="symbol">(</span><span class="normal">indent </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"("</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">].</span><span class="normal">key </span><span class="symbol">+</span><span class="normal"> </span><span class="string">")</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">);</span>
<span class="normal">                s</span><span class="symbol">.</span><span class="function">append</span><span class="symbol">(</span><span class="function">toString</span><span class="symbol">(</span><span class="normal">children</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">].</span><span class="normal">next</span><span class="symbol">,</span><span class="normal"> ht</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> indent </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"     "</span><span class="symbol">));</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> s</span><span class="symbol">.</span><span class="function">toString</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// comparison functions - make Comparable instead of Key to avoid casts</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">less</span><span class="symbol">(</span><span class="usertype">Comparable</span><span class="normal"> k1</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Comparable</span><span class="normal"> k2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> k1</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">k2</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">eq</span><span class="symbol">(</span><span class="usertype">Comparable</span><span class="normal"> k1</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Comparable</span><span class="normal"> k2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> k1</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">k2</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">BTree</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">BTree&lt;String, String&gt;</span><span class="normal"> st </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> BTree</span><span class="symbol">&lt;</span><span class="normal">String</span><span class="symbol">,</span><span class="normal"> String</span><span class="symbol">&gt;();</span>

<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.cs.princeton.edu"</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"128.112.136.12"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.cs.princeton.edu"</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"128.112.136.11"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.princeton.edu"</span><span class="symbol">,</span><span class="normal">    </span><span class="string">"128.112.128.15"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.yale.edu"</span><span class="symbol">,</span><span class="normal">         </span><span class="string">"130.132.143.21"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.simpsons.com"</span><span class="symbol">,</span><span class="normal">     </span><span class="string">"209.052.165.60"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.apple.com"</span><span class="symbol">,</span><span class="normal">        </span><span class="string">"17.112.152.32"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.amazon.com"</span><span class="symbol">,</span><span class="normal">       </span><span class="string">"207.171.182.16"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.ebay.com"</span><span class="symbol">,</span><span class="normal">         </span><span class="string">"66.135.192.87"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.cnn.com"</span><span class="symbol">,</span><span class="normal">          </span><span class="string">"64.236.16.20"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.google.com"</span><span class="symbol">,</span><span class="normal">       </span><span class="string">"216.239.41.99"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.nytimes.com"</span><span class="symbol">,</span><span class="normal">      </span><span class="string">"199.239.136.200"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.microsoft.com"</span><span class="symbol">,</span><span class="normal">    </span><span class="string">"207.126.99.140"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.dell.com"</span><span class="symbol">,</span><span class="normal">         </span><span class="string">"143.166.224.230"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.slashdot.org"</span><span class="symbol">,</span><span class="normal">     </span><span class="string">"66.35.250.151"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.espn.com"</span><span class="symbol">,</span><span class="normal">         </span><span class="string">"199.181.135.201"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.weather.com"</span><span class="symbol">,</span><span class="normal">      </span><span class="string">"63.111.66.11"</span><span class="symbol">);</span>
<span class="normal">        st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="string">"www.yahoo.com"</span><span class="symbol">,</span><span class="normal">        </span><span class="string">"216.109.118.65"</span><span class="symbol">);</span>


<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"cs.princeton.edu:  "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="string">"www.cs.princeton.edu"</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"hardvardsucks.com: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="string">"www.harvardsucks.com"</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"simpsons.com:      "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="string">"www.simpsons.com"</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"apple.com:         "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="string">"www.apple.com"</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"ebay.com:          "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="string">"www.ebay.com"</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"dell.com:          "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="string">"www.dell.com"</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"size:    "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">());</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"height:  "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">height</span><span class="symbol">());</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">st</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
